<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>提醒功能基本信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
    <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/css/public.css">
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
</head>
<body>

<form class="layui-form" style="margin: 15px;" lay-filter="sysPopupRemindDataForm">

    <div style="margin-left: 12%;">
        <div class="layui-form-item" style="display:none;">
            <label class="layui-form-label">主键</label>
            <div class="layui-input-block">
                <input type="text" name="id"  placeholder="请输入主键"
                       autocomplete="off" class="layui-input " maxlength="50" fieldname="id">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block" style="width: 75%">
                <input type="text" name="subject"  placeholder="请输入标题"
                       autocomplete="off" class="layui-input " lay-verify="required" maxlength="50" fieldname="subject">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">跳转URL</label>
            <div class="layui-input-block" style="width: 75%">
                <input type="text" name="url"  placeholder="请输入跳转URL"
                       autocomplete="off" class="layui-input " maxlength="50" fieldname="url"/>当用户点击提醒信息时的跳转地址
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">sql语句</label>
            <div class="layui-input-block" style="width: 75%">
                <textarea name="sql" id="sql" placeholder="请输入sql语句" lay-verify="required" autocomplete="off" class="layui-textarea" fieldname="sql"></textarea>
                <button class="layui-btn layui-icon layui-icon-ok" type="button" id="validSql">验证查询语句</button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数据源别名</label>
            <div class="layui-input-block" style="width: 75%">
                <div id="selectDataSourceAlias"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block" style="width: 75%">
                <input type="text" name="sn"  placeholder="请输入排序"
                       autocomplete="off" class="layui-input " lay-verify="sort" maxlength="50" fieldname="sn">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block" style="width: 75%">
                <!--<input type="text" name="enabled"  placeholder="请输入是否启用"-->
                       <!--autocomplete="off" class="layui-input " maxlength="50" fieldname="enabled">-->
                <select name="isEnabled">
                    <option value="0" selected>禁用</option>
                    <option value="1">启用</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block" style="width: 75%">
                <!--<input type="text" name="desc"  placeholder="请输入描述"
                       autocomplete="off" class="layui-input " maxlength="50" fieldname="desc">-->
                <textarea name="desc" id="desc" placeholder="请输入描述" autocomplete="off" maxlength="500" class="layui-textarea" fieldname="desc"></textarea>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div style="text-align: center;">
            <a lay-submit lay-filter="sysPopupRemind" class="layui-btn btn-n">提交</a>
            <a class="layui-btn layui-btn-danger cancelAddedit">返回</a>
        </div>
    </div>
</form>
<script src="${ctx}/public/component.js" type="text/javascript"></script>
<script src="${ctx}/layui/layui.js"></script>
<script>

    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','common','jquery','commons','form','setter','layer'], function () {
        var $ = layui.jquery;
        var commons = layui.commons;
        var form = layui.form;
        var setter = layui.setter;
        var common = layui.common;
        var layer = layui.layer;

        form.verify(commons.formVerify);

        $(".cancelAddedit").click(function () {
            $(this).attr("href", serverPath.systemPath+setter.sysLinkPath+"sysPopupRemind/view/list");
        });

        $.ajax({
            url:serverPath.systemPath + setter.sysLinkPath + 'sysPopupRemind/queryDataSourceList',
            type:"get",
            dataType:"json",
            success:function (data) {
                if(data.data != null){
                    var getData = data.data;
                    $("#selectDataSourceAlias").html("");
                    var html = '<select id="dataSource" name="dataSource"></select>';
                    $("#selectDataSourceAlias").append(html);
                    for(var i=0,c;c=getData[i++];){
                        if(i==0){
                            $("#dataSource").append('<option value="'+c.alias+'" selected>'+c.name+'</option>')
                        }else{
                            $("#dataSource").append('<option value="'+c.alias+'">'+c.name+'</option>')
                        }
                    }
                    form.render('select');
                }
            }

        });

        var id = "${id}";
        $.ajax({
            url:serverPath.systemPath + setter.sysLinkPath+ "sysPopupRemind/get",
            data:{id:id},
            type:"post",
            dataType:"json",
            success:function (data) {
                if(data.data != null){
                    var getData = data.data;
                    $("input[name='id']").val(getData.id);
                    $("input[name='subject']").val(getData.subject);
                    $("input[name='url']").val(getData.url);
                    $("#sql").val(getData.sql);
                    $("select[name='dataSource']").val(getData.dsalias).attr("selected","selected");
                    $("input[name='sn']").val(getData.sn);
                    $("select[name='isEnabled']").val(getData.isEnabled);
                    $("#desc").val(getData.desc);
                }
                form.render('select')
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert("获取数据失败！" + XMLHttpRequest.status);
            }
        });




        var index = parent.layer.getFrameIndex(window.name);
        form.on("submit(sysPopupRemind)",function () {
            var id = $("input[name='id']").val();
            var subject = $("input[name='subject']").val();
            var url = $("input[name='url']").val();
            var sql = $("#sql").val();
            var dsalias = $("select[name='dataSource']").val();
            var sn = $("input[name='sn']").val();
            var isEnabled = $("select[name='isEnabled']").val();
            var desc = $("#desc").val();
            var nowData = {"id": id,"subject": subject,"url":url,"sql": sql,"dsalias": dsalias,"sn": sn,"isEnabled": isEnabled, "desc":desc};
            $.ajax({
                url:serverPath.systemPath+ setter.sysLinkPath + "sysPopupRemind/save",
                type: "POST",
                dataType: 'json',
                contentType : 'application/json',
                data: JSON.stringify(nowData),
                success : function (data) {
                    if(commons._filterRecord(data)){
                        layer.alert(data.msg,function () {
                            location.href = serverPath.systemPath+setter.sysLinkPath+'sysPopupRemind/view/list' ;
                            layer.close(index);
                        });
                    }else{
                        layer.msg(data.msg);
                    }
                },
                error : function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("保存数据失败！" + XMLHttpRequest.status);
                }
            })
        });
        $("#validSql").click(function () {
            var sql = $("#sql").val();
            var dsAlias = $("select[name='dataSource']").val();
            var rollback = true;
            if($("#sql").val()==''){
            	layer.alert('请输入要校验的sql语句');
            }else{
            	 $.ajax({
                     url:serverPath.systemPath + setter.sysLinkPath + "sysPopupRemind/validSql",
                     type:"get",
                     dataType: 'json',
                     contentType : 'application/json',
                     data: {"sql":sql,"dsAlias":dsAlias},
                     success : function (data) {
                         if (commons._filterRecord(data)) {
                             layer.alert(data.msg,{skin: 'btn-class'}, function (index) {
                                 window.layer.close(index);
                             });
                         } else {
                             layer.alert(data.msg);
                         }
                     },
                     error : function (XMLHttpRequest, textStatus, errorThrown) {
                         layer.alert("保存数据失败！" + XMLHttpRequest.status);
                     }
                 })
            }
        });
    });
</script>
</body>
</html>